package com.xio.cloud.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: xio
 * @Description: 通用返回实体
 * @Date: 2020/9/24 15:10
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private static final Integer SUCCESS_CODE = 200;
    private static final Integer FAIL_CODE = 500;

    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }

    public static CommonResult ok() {
        return new CommonResult(SUCCESS_CODE, "操作成功");
    }

    public static CommonResult ok(String message, Object data) {
        return new CommonResult(SUCCESS_CODE, message, data);
    }

    public static CommonResult ok(Object data) {
        return new CommonResult(SUCCESS_CODE, "操作成功", data);
    }

    public static CommonResult fail(Object data) {
        return new CommonResult(FAIL_CODE, "操作失败", data);
    }

    public static CommonResult fail(String message, Object data) {
        return new CommonResult(FAIL_CODE, message, data);
    }

    public static CommonResult fail() {
        return new CommonResult(FAIL_CODE, "操作失败");
    }

    public static CommonResult of(boolean check, String message) {
        if (check) {
            return ok(message);
        }
        return fail();
    }

    public static CommonResult of(boolean check) {
        if (check) {
            return ok();
        }
        return fail();
    }

}
